/**
 * Copyright: Copyright (c) 2012
 * Company:anhosi.com
 * @author lifuyu
 * @date Aug 20, 2012
 * @version V1.0
 *
 * @Description: 
 */
package com.microcm.manage.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.microcm.system.entity.TagMenu;
import com.microcmframework.core.dao.SqlMapDao;
import com.microcmframework.core.entity.PageBean;

/**
 * 管理通用操作类
 * 
 * @author kansum
 * 
 */
@Repository("superDao")
public class SuperDao extends SqlMapDao
{
	public SuperDao()
	{
		super(SuperDao.class.getName());
	}

	/**
	 * 批量更新淘宝的省市区基础数据
	 * 
	 * @author：lifuyu
	 * @param addressLs
	 * @return
	 */
	public boolean batchCategorySave(final List<?> catList)
	{
		boolean ret = (Boolean) getSqlMapClientTemplate().execute(new SqlMapClientCallback<Object>()
		{
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException
			{
				executor.startBatch();
				for (Object o : catList)
				{
					Map<?, ?> m = (Map<?, ?>) o;
					String cid = (String) m.get("cid");

					Object co = executor.queryForObject("manage.isCategoryExist", cid);
					int c = (Integer) co;
					if (c == 0)
					{
						executor.insert("manage.insertCategory", m);
					}
				}
				int uc = executor.executeBatch();
				return uc > 0;
			}
		});
		return ret;
	}

	/**
	 * 批量删除商品类别数据
	 * 
	 * @author：lifuyu
	 * 
	 * @param cidList
	 * @return
	 */
	public boolean batchCategoryDelete(final List<Long> cidList)
	{
		boolean ret = (Boolean) getSqlMapClientTemplate().execute(new SqlMapClientCallback<Object>()
		{
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException
			{
				executor.startBatch();
				for (Long cid : cidList)
				{
					executor.insert("manage.deleteCategoryByCid", cid);
				}
				int uc = executor.executeBatch();
				return uc > 0;
			}
		});
		return ret;
	}

	/**
	 * 批量更新淘宝的省市区基础数据
	 * 
	 * @author：lifuyu
	 * @param addressLs
	 * @return
	 */
	public boolean batchMenuTagsLinkSave(final List<TagMenu> tagMenuList)
	{
		boolean ret = (Boolean) getSqlMapClientTemplate().execute(new SqlMapClientCallback<Object>()
		{
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException
			{
				executor.startBatch();
				for (TagMenu tagMenu : tagMenuList)
				{
					Object co = executor.queryForObject("manage.isTagMenuExist", tagMenu);
					int c = (Integer) co;
					if (c == 0)
						executor.insert("manage.insertTagMenu", tagMenu);
					else
						executor.update("manage.updateTagMenu", tagMenu);
				}
				int uc = executor.executeBatch();
				return uc > 0;
			}
		});
		return ret;
	}

	/**
	 * 批量更新淘宝的省市区基础数据
	 * 
	 * @author：lifuyu
	 * @param addressLs
	 * @return
	 */
	public boolean batchMenuTagsLinkRemove(final List<TagMenu> tagMenuList)
	{
		boolean ret = (Boolean) getSqlMapClientTemplate().execute(new SqlMapClientCallback<Object>()
		{
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException
			{
				executor.startBatch();
				for (TagMenu tagMenu : tagMenuList)
				{
					executor.update("manage.deleteTagMenu", tagMenu);
				}
				int uc = executor.executeBatch();
				return uc > 0;
			}
		});
		return ret;
	}

	/**
	 * 分页查询所有系统的配置项
	 * 
	 * @param qryParams
	 * @param pageBean
	 * @return
	 */
	public boolean queryAllSysConfig(PageBean pageBean)
	{
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put(_CAPACITY_QUERY_KEY, "manage.selectSysConfigsSize");
		paraMap.put(_ENTITY_QUERY_KEY, "manage.selectSysConfigsForPage");

		// 分页级别参数
		paraMap.put("pageFrom", pageBean.getPageSize() * (pageBean.getPage() - 1));
		paraMap.put("pageSize", pageBean.getPageSize());
		paraMap.put("sidx", pageBean.getSidx());
		paraMap.put("sord", pageBean.getSord());

		return this.queryForPage(paraMap, pageBean);
	}
}
